﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.Profile;
using System.Net;
using System.IO;

namespace EHTSaleManager.UIAdmin
{
    public partial class AdminCustomer : System.Web.UI.Page
    {
        private String UserName ="";
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                UserName = Request.QueryString["UserName"].ToString();
            }
            catch (Exception ex)
            {
                UserName = "";
            }
            if (!Page.IsPostBack)
            {
                MembershipUser theUser = Membership.GetUser(UserName.ToString());
                ProfileBase theProfile = ProfileBase.Create(UserName.ToString());

                this.lbAddress.Text = theProfile.GetPropertyValue("Address").ToString();
                this.lbCity.Text = theProfile.GetPropertyValue("City").ToString();
                this.lbEmail.Text = theProfile.GetPropertyValue("Email").ToString();
                this.lbFax.Text = theProfile.GetPropertyValue("Fax").ToString();
                this.lbFullName.Text = theProfile.GetPropertyValue("FirstName").ToString() + " " + theProfile.GetPropertyValue("LastName").ToString();
                this.lbPhone.Text = theProfile.GetPropertyValue("Phone").ToString();
                this.lbPostCode.Text = theProfile.GetPropertyValue("PostCode").ToString();
                this.lbRegion.Text = theProfile.GetPropertyValue("RegionID").ToString();
                this.lbServiceLevel.Text = theProfile.GetPropertyValue("ServiceLevelID").ToString();
                string SignatureUrl = theProfile.GetPropertyValue("SignatureUrl").ToString();

                if (string.IsNullOrEmpty(SignatureUrl) == false)
                {
                    if ((RemoteFileExists(@"~\ImageDatas\Signatures\" + SignatureUrl)) == false)
                    {
                        this.picSignature.ImageUrl = @"~\ImageDatas\Signatures\" + SignatureUrl;
                    }
                    else
                    {
                        this.picSignature.ImageUrl = @"~\ImageDatas\Signatures\noImage.gif";
                    }


                }
                else
                {
                    this.picSignature.ImageUrl = @"~\ImageDatas\Signatures\noImage.gif";
                }
                this.lbTotalAccount.Text = theProfile.GetPropertyValue("ToltalAccount").ToString();
            }
        }

        private bool RemoteFileExists(string url)
        {
            bool result = false;
            using (System.Net.WebClient client = new WebClient())
            {
                try
                {
                    Stream stream = client.OpenRead(url);
                    if (stream != null)
                    {
                        result = true;
                    }
                    else
                    {
                        result = false;
                    }
                }
                catch
                {
                    //Any exception will returns false.
                    result = false;
                }
            }
            return result;
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/UIAdmin/AdminCustomers.aspx");
        }
    }
}